package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.databinding.LayoutDialogAlertBinding;

public class AlertDialogUtils extends Dialog {

    private OnResultListener onResultListener;

    public ObservableField<String> title = new ObservableField<>();
    public ObservableField<String> message = new ObservableField<>();

    public void setOnResultListener(OnResultListener onResultListener) {
        this.onResultListener = onResultListener;
    }

    public AlertDialogUtils(@NonNull Context context, String title, String message) {
        super(context, R.style.myProgressDialogStyle);
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        this.title.set(title);
        this.message.set(message);
        LayoutDialogAlertBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_dialog_alert,
                null,
                false);
        binding.setUtils(this);
        setContentView(binding.getRoot());
        assert getWindow() != null;
        getWindow().getAttributes().width = context.getResources().getDisplayMetrics().widthPixels/3;
    }

    public void onCancel(){
        dismiss();
        if(onResultListener!=null)
            onResultListener.onCancel();
    }

    public void onOk(){
        dismiss();
        if(onResultListener!=null)
            onResultListener.onOk();
    }

    public interface OnResultListener{
        void onOk();
        void onCancel();
    }

}
